<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Store;
use app\model\StoreImage;
use app\model\Users;
use app\model\Member;
use app\model\Category;
use app\model\Openid;
use think\facade\Cache;
use app\model\StoreIncomelog;

class AdminstoreController extends Base
{

    public function detail()
    {
        if (Cache::store('redis')->get('store_' . UID())) {
            $data = null;
        } else {
            $data = Store::where(['uid' => UID()])->find();
            $this->updateuuid($data['uuid'], 'store');
        }

        if (!empty($data)) {
            $data = $data->toArray();
            $data['store_logo'] = toimg($data['store_logo']);

            $data['store_banner'] = StoreImage::field('image')
                ->where(['sid' => $data['id'], 'ptype' => 'banner'])
                ->order('sort asc')
                ->select()
                ->toArray();

            $data['content_img'] = StoreImage::field('image')
                ->where(['sid' => $data['id'], 'ptype' => 'content'])
                ->order('sort asc')
                ->select()
                ->toArray();
        } else {
            $uuid = $this->getuuid('store');
            if (!empty($uuid)) {
                $data = Store::where(['uuid' => $uuid])->find();
                if (!empty($data)) {
                    $data = $data->toArray();
                    $data['store_logo'] = toimg($data['store_logo']);

                    $data['store_banner'] = StoreImage::field('image')
                        ->where(['sid' => $data['id'], 'ptype' => 'banner'])
                        ->order('sort asc')
                        ->select()
                        ->toArray();

                    $data['content_img'] = StoreImage::field('image')
                        ->where(['sid' => $data['id'], 'ptype' => 'content'])
                        ->order('sort asc')
                        ->select()
                        ->toArray();
                }
            }
        }
        if (!Cache::store('redis')->get('store_' . UID())) {
            $data = 0;
            $message = '请先登录！';
            return $this->json(['message' => $message, 'data' => $data]);
        }
        if ($data['status'] != 1) {
            $data = 0;
            $message = '资料在审核中！';
        }
        if (!empty($data['environment_photo'])) {
            $data['environment_photo'] = json_decode($data['environment_photo']);
        }
        if (!empty($data['staff_photo'])) {
            $data['staff_photo'] = json_decode($data['staff_photo']);
        }
        if (!empty($data['store_photo'])) {
            $data['store_photo'] = json_decode($data['store_photo']);
        }
        return $this->json(['message' => $message, 'data' => $data]);
    }

    public function check()
    {
        $uuid = $this->getuuid();
        $data = Store::where(['uuid' => $uuid])->find();
        if ($data) {
            $data = $data->toArray();
        }
        $data['Category'] = Category::gettoparray();

        return $this->json(['data' => $data]);
    }
    public function logout()
    {
        $uuid = $this->getuuid('store');
        Openid::where(['uuid' => $uuid])->update(['status' => 1]);
        Cache::store('redis')->delete('store_' . UID());
        return $this->json(['message' => '退出成功！']);
    }

    public function addPhoto()
    {

        $uuid = $this->getuuid('store');
        $postdata = [];
        $store_photo = input('post.store_photo');
        if (!empty($store_photo)) {
            $postdata['store_photo'] = json_encode(explode(',', $store_photo));
        }
        $environment_photo = input('post.environment_photo');
        if (!empty($environment_photo)) {
            $postdata['environment_photo'] = json_encode(explode(',', $environment_photo));
        }
        $staff_photo = input('post.staff_photo');
        if (!empty($staff_photo)) {
            $postdata['staff_photo'] = json_encode(explode(',', $staff_photo));
        }
        Store::where(['uuid' => $uuid])->update($postdata);
        return $this->json(['message' => '保存成功！', 'data' => $postdata]);
    }

    public function doBindUser()
    {
        $id = input('post.id');
        $type = input('post.type');
        $user_id = UID();
        if ($type) {
            Store::where(['uid' => $user_id])->update(['uid' => 0]);
            Store::where(['id' => $id])->update(['uid' => $user_id]);
            // Cache::store('redis')->delete('store_' . UID());
            return $this->json(['message' => '绑定成功！']);
        } else {
            Store::where(['id' => $id])->update(['uid' => 0]);
            return $this->json(['message' => '解绑成功！']);
        }
    }
    public function incomelog()
    {
        $uuid = $this->getuuid('store');
        $ptype = input('post.ptype');

        $where['weid'] = weid();
        $where['sid'] = Store::where(['uuid' => $uuid])->value('id');
        if ($ptype) {
            $where['ptype'] = $ptype;
        }

        $data = StoreIncomelog::where($where)
            ->order('id desc')
            ->paginate(getpage())->each(function ($item, $key) {
                $item['create_time'] = time_format($item['create_time']);
                return $item;
            });
        return $this->json(['data' => $data]);
    }
}
